package com.issc.framework.helper;

import com.issc.framework.annotation.RequestMapping;
import com.issc.framework.annotation.RequestMethod;
import com.issc.framework.util.Handler;
import com.issc.framework.util.Request;
import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 获取handler（类和方法）的信息
 */
public final class ControllerHelper {
    private final static Map<Request, Handler> REQUEST_MAPPING_MAP = new HashMap<>();

    static {
        //获取所有的被Controller注解标注的类放入Set集合中
        Set<Class<?>> controllerClassSet = ClassHelper.getControllerClassSet();
        if (!controllerClassSet.isEmpty()) {
            for (Class<?> controllerClass : controllerClassSet) {
                String classMappingValue = null;
                //判断类是否被RequestMapping注解，如果是，则获取RequestMapping的值，放入变量classMappingValue
                if (controllerClass.isAnnotationPresent(RequestMapping.class)) {
                    RequestMapping requestMapping = controllerClass.getAnnotation(RequestMapping.class);
                    classMappingValue = requestMapping.value();
                }
                //获取Controller注解的类下面的所有方法，存入Method数组里
                Method[] methods = controllerClass.getDeclaredMethods();
                if (!ArrayUtils.isEmpty(methods)) {
                    for (Method method : methods) {
                        //如果方法被RequestMapping注解标注，则获取注解里的值，并与累上面RequestMapping的值相加得到值，存入value
                        //然后获取RequestMapping的method属性存入RequestMethod数组里
                        if (method.isAnnotationPresent(RequestMapping.class)) {
                            RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);

                            String value = classMappingValue + requestMapping.value();
                            RequestMethod[] requestMethod = requestMapping.method();
                            Request request;
                            //将RequestMapping的信息（类和类的方法）放入handler里
                            Handler handler = new Handler(controllerClass, method);
                            if (ArrayUtils.isNotEmpty(requestMethod)) {
                                for (RequestMethod requestMethod1 : requestMethod) {
                                    //封装请求信息（请求方法和请求路径）
                                    request = new Request(requestMethod1, value);
                                    REQUEST_MAPPING_MAP.put(request, handler);
                                }
                            }
                            //如果requestMethod为空，则设置默认所有的方法
                            else if (ArrayUtils.isEmpty(requestMethod)) {
                                requestMethod = new RequestMethod[]{RequestMethod.GET, RequestMethod.DELETE, RequestMethod.HEAD, RequestMethod.POST
                                        , RequestMethod.OPTIONS, RequestMethod.PATCH, RequestMethod.PUT, RequestMethod.TRACE};
                                for (RequestMethod requestMethod1 : requestMethod) {
                                    request = new Request(requestMethod1, value);
                                    REQUEST_MAPPING_MAP.put(request, handler);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 通过request来得到handler
     *
     * @param requestMethod 请求方法
     * @param requestPath   请求路径
     * @return REQUEST_MAPPING_MAP集合的value
     */
    public static Handler getHandler(RequestMethod requestMethod, String requestPath) {
        Request request = new Request(requestMethod, requestPath);
        return REQUEST_MAPPING_MAP.get(request);
    }
}
